const da = require("./../json/login.json");
const fs = require("fs");
const path = require("path");

module.exports = {
    //获取用户信息
    userInfo(req) {
        return da;
    },

    //重置密码
    resetPassword(req) {
        //这里用户名是唯一的
        let userName = req.userName;
        let flag = false;
        let box = da;
        console.log(req)
        da.forEach((e, i) => {
            if (e.userName == userName) {
                // 
                flag = true;
                box[i].userPsd = '123456';

            }
        })
        let newBox = JSON.stringify(box);
        if (flag) {
            // 重置密码
            fs.writeFile(path.join(__dirname, "./../json/login.json"), newBox, 'utf8', (err) => {
                if(err) {
                    console.log(err)
                   
                } else {
                    console.log("writeFile sucees")
                    
                }
            })
            return {
                code: "200",
                msg: "reset sucess"
            }

        } else {
            //重置失败，该用户不存=在
            return {
                        code: "404",
                        msg: "user is null"
                    }
            
        }
    },

    //添加用户或管理员
    add(req) {
        let userName = req.userName;
        let userPsd = req.userPsd;
        let identity = req.identity; 
        //判断用户是否存在
        let flag = false;
        console.log(identity)
        da.forEach(e => {
            console.log(e.userName, req.userName, e.identity, req.identity)
            if(e.userName == req.userName && e.identity == req.identity) {
                //用户存在
                flag = true;
            }
        })
        //用户存在  不能注册
        if(flag) {
            return {
                code: 401,
                msg: "user is exist"
            }
        }
        // 用户不存在  注册用户
        let newArr = da;
        newArr.push({
            userName,
            userPsd,
            identity,
            store: 1
        })
        fs.writeFile(path.join(__dirname, "./../json/login.json"), JSON.stringify(newArr), 'utf8', (err) => {
            if(err) {
                console.log(err)
                // codeData = {
                //     code: "402",
                //     msg: "write error"
                // }
            } else {
                console.log("writeFile sucees")
                // codeData = {
                //     code: "200",
                //     msg: "write sucess"
                // }
            }
        })
        return {
            code: 200,
            msg: "sign sucess"
        }
    },

    //冻结用户
    frezz(req) {
        let userName = req.userName;
        let position = 0;
        da.forEach((e, index) => {
            if(userName == e.userName) {
                position = index;
            }
        })

        da[position].store = 2;
        let box = JSON.stringify(da);
        fs.writeFile(path.join(__dirname, "./../json/login.json"), box , 'utf8', (err) => {
            if(err) {
                console.log(err)
            } else {
                console.log("writeFile sucees")
            }
        })
        return {
            code: 200,
            msg: "frezz sucess"
        }


    },

    //解冻
    unfrezz(req) {
        let userName = req.userName;
        let position = 0;
        da.forEach((e, index) => {
            if(userName == e.userName) {
                position = index;
            }
        })

        da[position].store = 1;
        let box = JSON.stringify(da);
        fs.writeFile(path.join(__dirname, "./../json/login.json"), box , 'utf8', (err) => {
            if(err) {
                console.log(err)
            } else {
                console.log("writeFile sucees")
            }
        })
        return {
            code: 200,
            msg: "unfrezz sucess"
        }
    },

    //删除用户
    del(req) {
        let userName = req.userName;
        let position = 0;
        da.forEach((e, index) => {
            if(userName == e.userName) {
                position = index;
            }
        })

        da.splice(position, 1);

        let box = JSON.stringify(da);
        fs.writeFile(path.join(__dirname, "./../json/login.json"), box , 'utf8', (err) => {
            if(err) {
                console.log(err)
            } else {
                console.log("writeFile sucees")
            }
        })
        return {
            code: 200,
            msg: "del sucess"
        }
    }

}